package appapi.controller.oider;

import appapi.Request.*;
import appapi.Vo.AddRoleVo;
import appapi.Vo.EditRoleVo;
import appapi.common.ResponseBean;
import appapi.common.UserLoginToken;
import appapi.entity.auth.RoleEntity;
import appapi.service.RoleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 角色信息
 *
 * @author lc
 * @version 1.0
 * @date 2023-05-17 10:08:27
 */
@Api(tags = "角色信息")
@RestController
@RequestMapping("/api/role")
public class RoleController {

    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);
    @Autowired
    private RoleService roleService;

    /**
     * 分页列表
     */
    @GetMapping("/getListByPage")
    @ApiOperation(value = "角色分页列表")
    @UserLoginToken
    public ResponseBean<RoleEntity> GetPageList(@Valid RolePageReq req){
        try {
            if (req.getCurrPage() == null || req.getPageSize() == null) {
                req.setCurrPage(1);
                req.setPageSize(10);
            }
            PageInfo<RoleEntity> pageInfo = roleService.page(req);
            return ResponseBean.success(pageInfo);
        } catch (Exception e) {
            logger.error("分页获取角色列表异常", e);
            return ResponseBean.fail("分页获取角色列表异常");
        }
    }

    /**
     * 选择列表
     */
    @GetMapping("/getRoleList")
    @ApiOperation(value = "角色列表")
    public ResponseBean<RoleEntity> list(@Valid RoleReq req){
        try {
            List<RoleEntity> dataList = roleService.list(req);
            return ResponseBean.success(dataList);
        } catch (Exception e) {
            logger.error("获取角色列表异常", e);
            return ResponseBean.fail("获取角色列表异常");
        }
    }

    /**
     * 信息
     */
    @GetMapping("/getRoleDetail")
    @ApiOperation(value = "角色详情")
    public ResponseBean<RoleEntity> detail(@Valid RoleDetailReq  roleDetailReq) {
        try {
            if (roleDetailReq.getRoleId() == null) {
                return ResponseBean.fail("角色ID不能为空");
            }
            RoleEntity entity = roleService.detail(roleDetailReq.getRoleId());
            if (entity != null) {
                return ResponseBean.success(entity);
            } else {
                return ResponseBean.fail("获取详情失败");
            }
        } catch (Exception e) {
            logger.error("获取角色详情异常", e);
            return ResponseBean.fail("获取角色详情异常");
        }
    }

    /**
     * 新增
     */
    @PostMapping(value = "/addRole")
    @ApiOperation(value = "新增角色信息")
    public ResponseBean insert(@Validated @RequestBody AddRoleVo roleVo){
        try {
            RoleEntity entity =roleVo.vo2Entity();;
            entity.setCreateTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            int count = roleService.count(entity);
            if (count >= 1) {
                return ResponseBean.fail("模块标识已存在，不能重复增加");
            }
            int iRet = roleService.insert(entity);
            if (iRet > 0) {
                return ResponseBean.success("保存成功");
            } else {
                return ResponseBean.fail("保存失败");
            }
        } catch (Exception e) {
            logger.error("新增角色异常", e);
            return ResponseBean.fail("新增角色异常");
        }
    }

    /**
     * 修改
     */
    @PostMapping(value = "/editRole")
    @ApiOperation(value = "修改角色信息")
    public ResponseBean update(@Validated @RequestBody EditRoleVo roleVo){
        try {
            RoleEntity entity =roleVo.vo2Entity();;
            entity.setUpdateTime(LocalDateTime.now());
            int count = roleService.count(entity);
            if (count >= 1) {
                return ResponseBean.fail("角色已存在，不能重复修改");
            }
            int iRet= roleService.update(entity);
            if (iRet > 0) {
                return ResponseBean.success("更新成功");
            } else {
                return ResponseBean.fail("更新失败");
            }
        } catch (Exception e) {
            logger.error("编辑角色异常", e);
            return ResponseBean.fail("编辑角色异常");
        }
    }

    /**
     * 删除
     */
    @PostMapping(value = "/deleteRole")
    @ApiOperation(value = "删除角色信息")
    public ResponseBean delete(@Validated @RequestBody DelRoleReq delRoleReq) {

        if (delRoleReq.getRoleId() == null) {
            return ResponseBean.fail("角色ID不能为空");
        }
        try {
            int iRet = roleService.delete(delRoleReq.getRoleId());
            if (iRet ==-1) {
                return ResponseBean.success("存在引用，不能删除！");
            }else if (iRet > 0) {
                return ResponseBean.success("删除成功");
            } else {
                return ResponseBean.fail("删除失败");
            }
        } catch (Exception e) {
            logger.error("删除角色异常", e);
            return ResponseBean.fail("删除角色异常");
        }
    }
}
